﻿using System;
using UnityEngine;

public static class Bezier
{
    /// <summary>
    /// 一阶贝塞尔
    /// </summary>
    /// <param name="p0"></param>
    /// <param name="p1"></param>
    /// <param name="t"></param>
    /// <returns></returns>
    public static Vector3 Bezier1(Vector3 p0, Vector3 p1, float t)
    {
        return (1 - t) * p0 + t * p1;
    }
    /// <summary>
    /// 二阶贝塞尔
    /// </summary>
    /// <param name="p0"></param>
    /// <param name="p1=贝塞尔曲线要拉伸的点"></param>
    /// <param name="p2"></param>
    /// <param name="t"></param>
    /// <returns></returns>
    public static Vector3 Bezier2(Vector3 p0, Vector3 p1, Vector3 p2, float t)
    {
        Vector3 B0 = (1 - t) * p0 + t * p1;
        Vector3 B1 = (1 - t) * p1 + t * p2;
        Vector3 B2 = (1 - t) * B0 + t * B1;
        return B2;
    }

    public static Vector3 Bezier3(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
    {
        float u = 1 - t;
        float tt = t * t;
        float uu = u * u;
        float uuu = uu * u;
        float ttt = tt * t;

        // 计算贝塞尔曲线点
        Vector3 p = uuu * p0; // 第一项
        p += 3 * uu * t * p1; // 第二项
        p += 3 * u * tt * p2; // 第三项
        p += ttt * p3; // 第四项

        return p;

    }
}